Skip to content

fix: /commit 命名空间按仓库 oid 格式校验完整长度,消除 64-hex 前缀别名 - #11

Closed
OrbitZore wants to merge 1 commit into
mainfrom
fix/commit-ns-oid-length
Closed

OrbitZore wants to merge 1 commit into
mainfrom
fix/commit-ns-oid-length

Conversation

@OrbitZore

Copy link
Copy Markdown
Owner

Closes #6

问题

安全审查实测抓获:SHA-1 仓库上访问 /commit/<64位小写hex>/...,
pathmap::is_full_oid(src/path_map.cpp:42)同时接受 40 与 64 hex 而放行,
随后 git_oid_fromstr(src/gitmount.cpp:480)在非实验构建下固定解析
恰好 40 个 hex 字符、不看余下 24 位——rc=0,得到该 64-hex 串
前 40 位前缀的 oid。若前缀恰为真实 commit(实测命中 HEAD),整棵树
被正常服务,违反 RFC 0000 §3.1:/commit 只接受与仓库对象格式一致的
完整小写 oid,其余应 ENOENT。定性为低危路径别名/规范松弛(前缀必须是
真实存在的 commit,无越权读写、无错误数据)。

实现

  • src/gitmount.cpp resolve_locked 的 Ns::Commit 分支:在
    is_full_oid 校验处叠加按仓库对象格式的长度校验:

    const std::size_t hexsz = repo_->oid_type() == GIT_OID_SHA1 ? 40 : 64;
    if (p.comps[0].size() != hexsz || !pathmap::is_full_oid(p.comps[0])) return -ENOENT;

    三元式与 rawobj.cpp:29 既有惯例一致;不引入对 libgit2 版本敏感的
    git_oid_hexsize。其余逻辑(tree 漫游、无 peeling、replace refs
    不生效)不动。

  • tests/integration/run_tests.sh:/commit (§3.1) 用例组追加三例,
    沿用脚本既有 check/test ! -e 断言模式:

    • 40-hex 真实 commit 目录可列举(既有行为回归);
    • 64-hex(取 $MAIN_TIP 拼 24 个 0)oid → ENOENT;
    • 同串深层路径 /commit/<64-hex>/src/hello.txt → ENOENT。
      fixture 仓库无需新形态,make_repo.sh 未改;用例位于 main mount
      段内,天然受 /dev/fuse 缺失时的整段跳过保护。
  • CHANGELOG.md:顶部新增 ## [Unreleased] / ### Fixed 英文条目。

附注:SHA-256 仓库本就无法挂载(非实验 libgit2 的 git_repository_open
对 sha256 仓库返回 unknown object format),64-hex 形态在当前构建下
只能表现为本别名缺陷;rawobj 的 GIT_OID_SHA256 分支作为正确的未来
铺垫保留不动(见 #6 关联说明)。

测试证据(红/绿)

本机 /dev/fuse + fusermount3 可用,集成测试实跑:

阶段 结果
修复前(临时 stash src 改动重建) FAIL - /commit rejects 64-hex oid (SHA-1 prefix alias)、FAIL - /commit rejects 64-hex deep path——旧代码确实命中前缀别名;passed: 113 / failed: 2
修复后 新用例全部 ok,passed: 115 / failed: 0 / skipped: 1(仅 root 专属 mount(8) 段)
单测 + 集成(ctest) 100% tests passed out of 2(-DCMAKE_BUILD_TYPE=Debug -DGITMOUNT_WERROR=ON)

改动文件已过 clang-format 18.1.8(项目锁定版本);bash 脚本与
CHANGELOG 不适用。单 commit,遵循 Conventional Commits。

风险

极低:新增的是更严格的前置拒绝(多拒一类本不该存在的地址形式),
不改变任何既有合法路径的解析结果;40-hex 全量集成用例与单测保持绿。
对 SHA-256 仓库(当前不可挂载)预留的 64 长度分支行为不变。

English summary

pathmap::is_full_oid accepts both 40- and 64-hex strings, but the
non-experimental libgit2 git_oid_fromstr parses exactly 40 hex digits
and ignores the rest — on a SHA-1 repository a 64-hex /commit/<oid>
path silently resolved to the commit named by its 40-char prefix
(verified live against HEAD, issue #6). RFC 0000 §3.1 requires the full
oid to match the repository's object format; anything else must ENOENT.

Fix: in the Ns::Commit branch of resolve_locked, validate the
component length against repo_->oid_type() (40 for SHA-1, 64
otherwise — same ternary convention as rawobj.cpp:29, avoiding the
version-sensitive git_oid_hexsize) before parsing. Tree walking, no
peeling and replace-refs passthrough are untouched.

Tests: three new integration assertions in the /commit group — a real
40-hex commit dir still lists, while a 64-hex oid (real oid + 24 zeros)
and its deep path are ENOENT. Red/green: without the fix the two new
cases fail (113 passed / 2 failed); with it 115 passed / 0 failed
(skipped: 1, root-only mount(8) section). Unit + integration ctest
green under -DGITMOUNT_WERROR=ON. No fixture changes needed; CHANGELOG
Unreleased/Fixed entry added.

Closes #6

问题(#6):pathmap::is_full_oid 同时接受 40 与 64 位小写 hex,而 SHA-1
仓库上非实验构建的 git_oid_fromstr 恰好解析 40 个字符、无视余下 24 位
——/commit/<64-hex> 被静默解析为其前 40 位前缀命中的 commit(实测命中
HEAD),违反 RFC 0000 §3.1"完整小写 oid,长度须与仓库对象格式一致"。

实现:resolve_locked 的 Ns::Commit 分支在 is_full_oid 之外叠加按
repo_->oid_type() 的长度校验(SHA-1=40 / 其他=64,三元式与
rawobj.cpp:29 既有惯例一致,不引入版本敏感的 git_oid_hexsize);长度
不符即 ENOENT。其余逻辑(tree 漫游、无 peeling、replace refs 不生效)
不动。附注:SHA-256 仓库本就无法挂载(git_repository_open 返回
"unknown object format"),64-hex 形态当前只能表现为本别名缺陷。

测试:tests/integration/run_tests.sh 追加三例——40-hex 真实 commit
目录可列举(回归)、64-hex(40-hex+24 个 0)oid 与深层路径均 ENOENT;
红/绿验证:无修复时新用例 2 FAIL(113 passed / 2 failed),加修复后
115 passed / 0 failed(本机 /dev/fuse + fusermount3 实跑)。

CHANGELOG:Unreleased/Fixed 记录。

Closes #6
@github-actions

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had activity in 14 days. It will be closed in 7 days if no further activity occurs.

@github-actions github-actions Bot added the Stale label Nov 16, 2026
@github-actions

Copy link
Copy Markdown

Closed automatically due to inactivity. Feel free to reopen.

@github-actions github-actions Bot closed this Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

/commit/<64-hex> 在 SHA-1 仓库上被静默截断为 40 位前缀 commit(RFC 0000 §3.1 违反)

1 participant